﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using TrinityAPI.Base.Tools;

namespace TrinityAPI.Base
{
    public class Log : IAction
    {
        public JToken Invoke(JObject dataset, params JToken[] args)
        {
            string message = args.Length > 0 ? args[0]?.ToString() : null;
            string loggerName = args.Length > 1 ? args[1]?.ToString() : null;
            string level = (args.Length > 2 ? args[2]?.ToString() : null ?? string.Empty).ToLower();

            if (!string.IsNullOrWhiteSpace(message))
            {
                switch (level)
                {
                    case "error":
                        LogHelper.Error(message, new Exception(message), loggerName);
                        break;
                    case "warn":
                        LogHelper.Warn(message, loggerName);
                        break;
                    case "debug":
                        LogHelper.Debug(message, loggerName);
                        break;
                    default:
                        LogHelper.Info(message, loggerName);
                        break;
                }
                return true;
            }

            return false;
        }
    }
}
